﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CarwashLibrary.CW_Storage
{
    public class CW_Storage
    {
        /// <summary>
        /// method gets the password associated with this PIN
        /// </summary>
        /// <param name="PIN"></param>
        /// <returns></returns>
        public string getPassword(int PIN)
        {
            string[] line = this.readFile(PIN);
            return line[2];
        }

        /// <summary>
        /// method searches the type of the entered PIN
        /// </summary>
        /// <param name="PIN"></param>
        /// <returns>type of PIN entered</returns>
        public string[] getPINData(int PIN)
        {
            string[] line = readFile(PIN);
            return line;
        }

        private string[] readFile(int PIN)
        {
            try
            {
                using (StreamReader sr = new StreamReader("storage/storage.txt"))
                {
                    bool pinFound = false;
                    while (!pinFound)
                    {
                        String line = sr.ReadLine();
                        string[] words = line.Split(' ');
                        if (Convert.ToInt32(words[0]) == PIN)
                        {
                            return words;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            return null;
        }

        /// <summary>
        /// @override. adds a record to permanent storage 
        /// </summary>
        /// <param name="PIN"></param>
        /// <param name="pinType"></param>
        /// <returns>returns whether the operation was successful</returns>
        public bool addRecord(int PIN, int pinType)
        {
            return addRecord(PIN, pinType, null);
        }

        /// <summary>
        /// adds a record to permanent storage 
        /// </summary>
        /// <param name="PIN"></param>
        /// <param name="pinType"></param>
        /// <returns>returns whether the operation was successful</returns>
        public bool addRecord(int PIN, int pinType, string password)
        {
            return false;
        }

        /// <summary>
        /// add a new log entry to permanent storage
        /// </summary>
        /// <param name="newLog"></param>
        /// <returns>returns whether the operation was successful</returns>
        public bool addLogEntry(string newLog)
        {
            return false;
        }

        public ArrayList retrieveLogs()
        {
            try
            {
                if (new FileInfo("storage/logs.txt").Length != 0)
                {
                    using (StreamReader sr = new StreamReader("storage/logs.txt"))
                    {
                        ArrayList logs = new ArrayList();
                        String line = sr.ReadLine();
                        while (line != null)
                        {
                            logs.Add(line);
                            line = sr.ReadLine();
                        }
                        return logs;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            return null;
        }
    }
}
